package com.example.controller;

import com.example.common.Result;
import com.example.dto.LoginResponse;
import com.example.entity.User;
import com.example.service.UserService;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true")
public class LoginController {
    private final UserService userService;

    public LoginController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping("/login")
    public Result<LoginResponse> login(@RequestBody User user) {
        LoginResponse loginResponse = userService.login(user.getUsername(), user.getPasswordHash());
        if (loginResponse != null) {
            return Result.success(loginResponse);
        }
        return Result.error("用户名或密码错误");
    }
} 